package object_ManPower;

import javax.swing.*;

public class Manpower {
    public ManPowerData[] manPower = new ManPowerData[10];
    public int number = 0;

    public void login(){
        for (int i=0;i<3;i++) {
            String inCount = JOptionPane.showInputDialog(null,"输入登录账号");
            String inPwd = JOptionPane.showInputDialog(null,"输入登录密码");
            if (inCount.equals("java")&&inPwd.equals("123")) {
                JOptionPane.showMessageDialog(null, "登录成功");
                break;
            }
            JOptionPane.showMessageDialog(null,"账号或密码有误，重新输入");
            if (i==2){
                JOptionPane.showMessageDialog(null,"非法登录");
                System.exit(0);
            }
        }
    }

    public void add(){
        ManPowerData data1 = new ManPowerData();
        String name = JOptionPane.showInputDialog(null, "输入添加员工姓名");
        String jobNum = JOptionPane.showInputDialog(null, "输入添加员工工号");
        String job = JOptionPane.showInputDialog(null, "输入添加员工职位");
        int salary = Integer.parseInt(JOptionPane.showInputDialog(null, "输入添加员工工资"));
        data1.name = name;
        data1.job = job;
        data1.salary = salary;
        data1.jobNum = jobNum;
        manPower[number] = data1;
        number++;

        if (number == manPower.length) {
            this.addArray();
        }
    }


    //数组扩容
    public void addArray(){
        ManPowerData[] newManPower = new ManPowerData[number+10];
        for (int i=0;i<number;i++){
            newManPower[i] = manPower[i];
        }
        manPower = newManPower;
    }

    //查找员工
    public int search(String key){
        int index = -1;
        for (int i=0;i<number;i++){
            if (key.equals(manPower[i].jobNum)||key.equals(manPower[i].name)){
                index = i;
                break;
            }
        }
        return index;
    }

    //信息展示
    public void informShow(){
        String result = "姓名  工号  职位   工资\n";
        for (int i=0;i< number;i++)
            result += manPower[i].name+"  "+manPower[i].jobNum+"  "+manPower[i].job+"   "+manPower[i].salary+"\n";
        JOptionPane.showMessageDialog(null, result);
    }

    //删除员工
    public void delete(){
        String str = JOptionPane.showInputDialog(null,"输入要删除的员工姓名或工号");
        int aa = this.search(str);
        if (aa == -1){
            JOptionPane.showMessageDialog(null,"查无此人");
        }else {
            for (int i=aa;i<number-1;i++){
                manPower[i] = manPower [i+1];
            }
            number--;
        }
        this.informShow();
    }

    //修改员工工资
    public void changSalary(){
        String str = JOptionPane.showInputDialog(null,"输入要修改工资员工的姓名或工号");
        int num = this.search(str);
        if (num == -1){
            JOptionPane.showMessageDialog(null,"查无此人");
        }
        if (num!=-1){
            int mon = Integer.parseInt(JOptionPane.showInputDialog(null,"输入修改金额"));
            manPower[num].salary += mon;
        }
    }
}
